import { PASS_UP_LV } from '../../config/play_def'
module.exports = {
    taskInit() {
        let pass_list = he.local.getJson('pass_list', 'user')
        if (pass_list) {
            this.passLen = pass_list.length || 1
        } else {
            this.passLen = 1
        }
        // this.passLen=51
        this.mission = []
        for (let i = 9001; i < 9005; i++) {
            this.checkTaskType(i)
        }
        he.local.setJson('task', this.mission, 'user')
    },
    checkTaskType(type) {
        //每日任务次数需求
        let typeNeed = 0
        //已经生成的该类型任务的数量
        let typeHave = 0
        let taskGrp = he.db.getTable('Task')

        for (let j = 0; j < taskGrp.length; j++) {
            if (taskGrp[j].type == type) {
                typeNeed = taskGrp[j].day
                break
            }
        }

        for (let i = 0; i < this.mission.length; i++) {
            if (this.mission.type == type) {
                typeHave++
            }
        }
        if (typeHave < typeNeed) {
            this.getTypeTask(type, (typeNeed - typeHave))
        }
    },
    getTypeTask(type, num) {
        let taskGrp = he.db.getTable('Task')
        // let monsterGrp = he.db.getTable('Monster')
        let typeGrp = []
        for (let i = 0; i < taskGrp.length; i++) {
            //同类型 且关卡梯度符合条件
            if (taskGrp[i].type == type) {
                if (taskGrp[i].param) {
                    if (taskGrp[i].param.split('#')[0] <= this.passLen) {
                        typeGrp.push(taskGrp[i])
                    }
                } else {
                    typeGrp.push(taskGrp[i])
                }

            }
        }
        for (let j = 0; j < num; j++) {
            let ranTask = typeGrp[this.randomNum(0, (typeGrp.length - 1))]
            let zanMission = { type: type, num: 0, need: ranTask.num, reward: ranTask.reward, isGet: false }
            // switch (type) {
            //     case 9001:
            //         break
            //     case 9002:
            //         let level = (Math.ceil(this.passLen / PASS_UP_LV) + parseInt(ranTask.param.split('#')[1]))
            //         zanMission.param = this.randomMonster(level)
            //         break
            //     case 9003:
            //         zanMission.param = this.randomRole()
            //         break
            //     case 9004:
            //         break
            //     default:
            //         break
            // }
            // this.mission.push(zanMission)
            switch (type) {
                case 9001:
                    this.mission.push(zanMission)
                    break
                case 9002:
                    // let level = (Math.ceil(this.passLen / PASS_UP_LV) + parseInt(ranTask.param.split('#')[1]))
                    // zanMission.param = this.randomMonster(level)
                    // this.mission.push(zanMission)
                    this.getTask9002(typeGrp)
                    break
                case 9003:
                    this.getTask9003(typeGrp)
                    break
                case 9004:
                    this.mission.push(zanMission)
                    break
                default:
                    break
            }

        }
    },

    getTask9002(typeGrp){
        let ranTask = typeGrp[this.randomNum(0, (typeGrp.length - 1))]
        let zanMission = { type: typeGrp[0].type, num: 0, need: ranTask.num, reward: ranTask.reward, isGet: false }
        let level = (Math.ceil(this.passLen / PASS_UP_LV) + parseInt(ranTask.param.split('#')[1]))
        zanMission.param = this.randomMonster(level)
        let canSave = this.checkTask(zanMission)
        if (!canSave) {
            this.getTask9002(typeGrp)
        } else {
            this.mission.push(zanMission)
        }
    },

    getTask9003(typeGrp) {
        let ranTask = typeGrp[this.randomNum(0, (typeGrp.length - 1))]
        let zanMission = { type: typeGrp[0].type, num: 0, need: ranTask.num, reward: ranTask.reward, isGet: false }
        zanMission.param = this.randomRole()
        let canSave = this.checkTask(zanMission)
        if (!canSave) {
            this.getTask9003(typeGrp)
        } else {
            this.mission.push(zanMission)
        }
    },

    checkTask(task) {
        let isHave = false
        for (let i = 0; i < this.mission.length; i++) {
            if (this.mission[i].type == task.type) {
                if (this.mission[i].need == task.need && this.mission[i].param == task.param) {
                    isHave = true
                    return false
                }
            }
        }
        if (!isHave) {
            return true
        }
    },


    randomRole() {
        //角色先写死
        let roleGrp = [1001, 1002, 1003, 1004, 1005]
        for (let i = 0; i < roleGrp.length; i++) {
            return roleGrp[this.randomNum(0, (roleGrp.length - 1))]
        }
    },
    randomMonster(level) {
        let monsterGrp = he.db.getTable('Monster')
        let monGrp = []
        for (let i = 0; i < monsterGrp.length; i++) {
            if (monsterGrp[i].rank == level) {
                monGrp.push(monsterGrp[i])
            }
        }
        return monGrp[this.randomNum(0, (monGrp.length - 1))].monsterId
    },
    //范围内的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    },
}